package com.woniuxy.cglibproxy;

import com.woniuxy.aspect.MyAspect;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;

public class CglibProxyFactory {
    public static Object getCglibProxy(Object targetClass){
        Object o = Enhancer.create(targetClass.getClass(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //前置增强
                MyAspect myAspect = new MyAspect();
                myAspect.beforeLog();
                //目标方法
                Object invoke = method.invoke(targetClass, args);
                myAspect.afterLog();
                return invoke;
            }
        });
        return o;
    }
}
